package com.takeshequ;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;

import com.andreabaccega.widget.FormEditText;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.takeshequ.homepage.Member;
import com.takeshequ.main.MainFragmentActivity;
import com.utils.BasicAsyncHttpResponseHandler;
import com.utils.HttpClientUtils;
import com.utils.HttpClientUtils.HttpResponseHandler;

public class LoginActivity extends BasicActivity {
	private FormEditText phonenoText, pwdText;
	private ImageButton loginButton;
	@ViewInject(id = R.id.top_title)
	TextView titleView;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		setViews();
		setButtonEvents();
	}

	@Override
	protected void onResume() {
		super.onResume();
		titleView.setText("登录");
	}

	private void setViews() {
		// topImage.setBackgroundResource(R.drawable.top_login);
		phonenoText = (FormEditText) findViewById(R.id.login_phoneno);
		pwdText = (FormEditText) findViewById(R.id.login_pwd);
		loginButton = (ImageButton) findViewById(R.id.login_login_btn);

	}

	private void setButtonEvents() {
		loginButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (phonenoText.testValidity() && pwdText.testValidity()) {
					login(phonenoText.getEditableText().toString(), pwdText
							.getEditableText().toString());
				}
			}
		});
	}
	private void getPersonInfo() {

		AsyncHttpClient client = new AsyncHttpClient();
		Map<String, String> params = new HashMap<String, String>();
		params.put("memberId", Constants.memberId);
		client.get(Constants.domainAndPort + "/memberinfo", new RequestParams(
				params), new BasicAsyncHttpResponseHandler(
				LoginActivity.this, "", "正在连接,请稍后...") {
			@Override
			public void onSuccessCustomer(String response) {
				try {
					JSONObject jsonObject = new JSONObject(response);
					Gson gson = new Gson();
					Constants.member = gson.fromJson(
							jsonObject.getString("member"), Member.class);
					Intent intent = new Intent(LoginActivity.this,
							MainFragmentActivity.class);
					startActivity(intent);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		});

	}

	private void login(final String phoneno, final String pwd) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("nickName", sharedPreferencesUtils.getNickName());
		params.put("loginName", phoneno);
		params.put("password", pwd);
		HttpClientUtils.post(LoginActivity.this, "正在登录,请稍后", "login", params,
				new HttpResponseHandler() {

					@Override
					public void handler(String response) {
						ResponseBody responseBody = gson.fromJson(response,
								ResponseBody.class);
						if (responseBody.getState().equals("1")) {
							sharedPreferencesUtils.setMemberId(responseBody
									.getMember().getMemberId());
							sharedPreferencesUtils.setPhone(phoneno);
							sharedPreferencesUtils.setPwd(pwd);
							sharedPreferencesUtils.setNickName(responseBody
									.getMember().getNickName());
							Constants.memberId = responseBody.getMember()
									.getMemberId();
							JPushInterface.setAliasAndTags(LoginActivity.this,
									responseBody.getMember().getMemberId(),
									null);
							getPersonInfo();
						} else {
							Toast.makeText(LoginActivity.this,
									responseBody.getDesc(), 2000).show();
						}
					}
				});

	}

}
